package cn.com.shopec.erp.multiChannel.dao.impl;

import java.math.BigInteger;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;
import cn.com.shopec.erp.common.orm.impl.HibernateEntityDao;
import cn.com.shopec.erp.common.page.PageFinder;
import cn.com.shopec.erp.common.page.PageQuery;
import cn.com.shopec.erp.common.utils.StringUtil;
import cn.com.shopec.erp.multiChannel.dao.IChannelOrderDao;
import cn.com.shopec.erp.multiChannel.entity.ChannelOrder;
import cn.com.shopec.erp.multiChannel.vo.ChannelOrderVo;

@Repository
public class ChannelOrderDaoImpl extends HibernateEntityDao<ChannelOrder> implements IChannelOrderDao {

	/**
	 * 根据订单编号查找订单
	 * 
	 * @param orderId
	 * @return
	 */
	public ChannelOrder queryByOrderId(String orderId) {
		// Assert.notNull(orderId, "value is required");
		// String sql="SELECT * FROM tbl_channel_order where
		// channel_order_id="+orderId;
		// Query query =this.createSQLQuery(sql);
		// return query.list().size()>0?(ChannelOrder)query.list().get(0):null;
		Assert.notNull(orderId, "orderId is required");
		ChannelOrder order = this.get("channelOrderId", orderId);
		return order;
	}

	/**
	 * 条件查找订单
	 * 
	 * @param orderId
	 * @return
	 */
	public List queryByOrder(ChannelOrder order) {
		String params = "";
		if (StringUtil.isExist(order.getOrderState())) {
			params += " and order_state='" + order.getOrderState() + "'";
		}
		if (StringUtil.isExist(order.getPayType())) {
			params += " and pay_type='" + order.getPayType() + "'";
		}
		if (StringUtil.isExist(order.getChannelOrderType())) {
			params += " and channel_order_type='" + order.getChannelOrderType() + "'";
		}
		if (StringUtil.isExist(order.getChannelOrderId())) {
			params += " and channel_order_id='" + order.getChannelOrderId() + "'";
		}
		/*
		 * if(StringUtil.isExist(order.getOrderStartTimeBegin())){ params +=
		 * " and order_start_time > '"+order.getOrderStartTimeBegin()+"'"; }
		 * if(StringUtil.isExist(order.getOrderStartTimeEnd())){ params +=
		 * " and order_start_time < '"+order.getOrderStartTimeEnd()+"'"; }
		 */
		String sql = "SELECT * FROM tbl_channel_order where 1=1 " + params;
		sql += "order by order_start_time desc";
		try {
			Query query = this.createSQLQuery(sql);
			return query.list();
		} catch (HibernateException e) {
			System.out.println(e.getMessage());
			e.printStackTrace();
			return null;
		}
	}

	/**
	 * 分页查询渠道订单
	 * 
	 * @param orderId
	 * @return
	 */
	public PageFinder<ChannelOrder> productStockPaged(ChannelOrder order, PageQuery pageQuery) {
		String params = "";
		if (StringUtil.isExist(order.getOrderState())) {
			params += " and order_state='" + order.getOrderState() + "'";
		}
		if (StringUtil.isExist(order.getPayType())) {
			params += " and pay_type='" + order.getPayType() + "'";
		}
		if (StringUtil.isExist(order.getChannelOrderType())) {
			params += " and channel_order_type='" + order.getChannelOrderType() + "'";
		}
		if (StringUtil.isExist(order.getChannelOrderId())) {
			params += " and channel_order_id='" + order.getChannelOrderId() + "'";
		}
		/*
		 * if(StringUtil.isExist(order.getOrderStartTimeBegin())){ params +=
		 * " and order_start_time > '"+order.getOrderStartTimeBegin()+"'"; }
		 * if(StringUtil.isExist(order.getOrderStartTimeEnd())){ params +=
		 * " and order_start_time < '"+order.getOrderStartTimeEnd()+"'"; }
		 */
		String sql = "SELECT * FROM tbl_channel_order where 1=1 " + params + " order by order_start_time desc";
		String countSql = "SELECT count(1) FROM tbl_channel_order where 1=1 " + params;
		String limitsql = sql + " limit " + (pageQuery.getPage() - 1) * pageQuery.getPageSize() + ","
				+ pageQuery.getPageSize();
		int count = this.createSQLQuery(countSql).list().size();
		List<ChannelOrder> list = (List<ChannelOrder>) getSession().createSQLQuery(limitsql)
				.addEntity(ChannelOrder.class).list();
		PageFinder<ChannelOrder> page = new PageFinder<ChannelOrder>(pageQuery.getPage(), pageQuery.getPageSize(),
				count, list);
		return page;
	}

	@Override
	public int updateByChannelOrderId(ChannelOrder channelOrder) {
		String params = "";
		if (StringUtil.isExist(channelOrder.getOrderState())) {
			if (StringUtil.isExist(params)) {
				params += ",order_state='" + channelOrder.getOrderState() + "'";
			} else {
				params += " order_state='" + channelOrder.getOrderState() + "'";
			}
		}
		if (StringUtil.isExist(channelOrder.getReturnState())) {
			if (StringUtil.isExist(params)) {
				params += ",return_state='" + channelOrder.getReturnState() + "'";
			} else {
				params += " return_state='" + channelOrder.getReturnState() + "'";
			}
		}
		if (StringUtil.isExist(params)) {
			String sql = "update tbl_channel_order set " + params + " where channel_order_id='"
					+ channelOrder.getChannelOrderId() + "'";
			return this.createSQLQuery(sql).executeUpdate();
		} else {
			return 0;
		}
	}

}
